package com.ocean.web.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.ocean.common.commons.OceanCommonError;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: FuGui Wang
 * @Description: Swagger配置类
 * @Date 2022/12/1
 * @Version V1.1.0
 */
@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@Component
public class SwaggerConfig {

    // 是否开启swagger
    @Value("${swagger.enabled}")
    private boolean enabled;

    /**
     * 云平台系统管理模块接口
     *
     * @return 返回 Docket
     */
    @Bean
    public Docket systemApi() {
        return getDocket("云平台Web接口",
                new String[]{"/.*"}
        );
    }

    /**
     * 设置API注入
     *
     * @param groupName 组名
     * @param paths     正则路径匹配
     * @return 返回 Docket
     */
    private Docket getDocket(String groupName, String[] paths) {
        List<ResponseMessage> respMsg = new ArrayList<>();
        Arrays.stream(OceanCommonError.values()).forEach(emBusinessError -> respMsg.add(
                new ResponseMessageBuilder().code(emBusinessError.getErrCode()).message(emBusinessError.getErrMsg()).build()));

        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2).enable(enabled).apiInfo(apiInfo())
                .globalResponseMessage(RequestMethod.GET, respMsg)
                .globalResponseMessage(RequestMethod.POST, respMsg)
                .globalResponseMessage(RequestMethod.PUT, respMsg)
                .globalResponseMessage(RequestMethod.DELETE, respMsg)
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ocean")).paths(PathSelectors.any());
        if (paths == null) {
            apiSelectorBuilder.paths(PathSelectors.any());
        } else {
            StringBuilder pathRegex = new StringBuilder();
            for (String path : paths) {
                pathRegex.append("(").append(path).append(")|");
            }
            apiSelectorBuilder.paths(PathSelectors.regex(pathRegex.substring(0, pathRegex.length() - 1)));
        }
        return apiSelectorBuilder.build();
    }

    /**
     * 设置API基础信息
     *
     * @return 返回 ApiInfo
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("国家海洋预报云服务平台接口文档")
                .description("国家海洋预报云服务平台V1.0")
                .contact(new Contact("云平台", "http://localhost:9001", "xxx"))
                .termsOfServiceUrl("http://localhost:9001")
                .version("1.0")
                .build();
    }
}
